package cn.com.connext.scrm.web.rest;

import cn.com.connext.scrm.service.mapper.WhiskeyGiftsService;
import cn.com.connext.scrm.web.rest.errors.MyErrorCode;
import cn.com.connext.scrm.web.rest.req.ActivityCreateReq;
import cn.com.connext.scrm.web.rest.req.AwardRecordUpdateReq;
import cn.com.connext.scrm.web.rest.req.DeleteReq;
import cn.com.connext.scrm.web.rest.req.WhiskeyGiftsCreateReq;
import cn.com.connext.scrm.web.rest.resp.WhiskeyAwardsResp;
import cn.com.connext.scrm.web.rest.resp.WhiskeyGiftsResp;
import com.alibaba.fastjson.JSON;
import com.connext.common.exception.ServiceException;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 威士忌之星活动礼物配置
 * 特殊活动：礼品盒中奖人只有一条
 */
@RestController
@RequestMapping("/api/whiskey")
public class WhiskeyGiftsResource {
    private final Logger log = LoggerFactory.getLogger(WhiskeyGiftsResource.class);

    @Autowired
    private WhiskeyGiftsService whiskeyGiftsService;

    /**
     * 当前当期启用活动礼物配置
     */
    @PostMapping("/now-gifts")
    @ApiOperation(value = "前当期启用活动礼物配置", tags = {"威士忌之星活动礼物列表"}, notes = "前当期启用活动礼物配置")
    public ResponseEntity<List<WhiskeyGiftsResp>> queryNowGifts() {
        log.info("查询威士忌之星活动当前当期启用活动礼物配置");
        return whiskeyGiftsService.queryNowGifts();
    }

    @PostMapping("/gifts-create")
    @ApiOperation(value = "创建问答活动", tags = {"问答活动列表"}, notes = "创建问答活动")
    public ResponseEntity<Boolean> create(@RequestBody WhiskeyGiftsCreateReq req) {
        log.info("创建问答活动，入参req : {}", JSON.toJSONString(req));
        if (req == null) {
            throw new ServiceException(MyErrorCode.ERROR_INPUT);
        }
        return whiskeyGiftsService.create(req);
    }

    @PostMapping("/gifts-detail")
    @ApiOperation(value = "创建问答活动", tags = {"问答活动列表"}, notes = "创建问答活动")
    public ResponseEntity<WhiskeyGiftsResp> detail(@RequestBody DeleteReq req) {
        log.info("创建问答活动，入参req : {}", JSON.toJSONString(req));
        if (StringUtils.isBlank(req.getId())) {
            throw new ServiceException(MyErrorCode.ERROR_INPUT);
        }
        return whiskeyGiftsService.detail(req.getId());
    }
    /**
     * 当前当期中奖人信息
     */
    @PostMapping("/now-awards")
    @ApiOperation(value = "前当期启用活动礼物配置", tags = {"威士忌之星活动礼物列表"}, notes = "前当期启用活动礼物配置")
    public ResponseEntity<WhiskeyAwardsResp> queryNowAward() {
        log.info("查询威士忌之星活动当前当期启用活动礼物配置");
        return whiskeyGiftsService.queryNowAward();
    }
    /**
     * 帖子参与威士忌之星活动设置中奖人
     */
    @PostMapping("/award-record")
    @ApiOperation(value = "更新奖励记录表", tags = {"奖励记录表"}, notes = "更新奖励记录表")
    public ResponseEntity updateAwardRecord(@RequestBody AwardRecordUpdateReq req) {
        log.info("帖子参与威士忌之星活动,参数req={}", JSON.toJSONString(req));
        if (StringUtils.isBlank(req.getNotId()) || StringUtils.isEmpty(req.getActId())) {
            throw new ServiceException(MyErrorCode.ERROR_INPUT);
        }
        return whiskeyGiftsService.updateAwardRecord(req);
    }

    /**
     * 查询威士忌之星活动设置中奖人
     */
    @PostMapping("/query-winner")
    @ApiOperation(value = "更新奖励记录表", tags = {"奖励记录表"}, notes = "更新奖励记录表")
    public ResponseEntity queryAwardRecord(@RequestBody AwardRecordUpdateReq req) {
        log.info("帖子参与威士忌之星活动,参数req={}", JSON.toJSONString(req));
        if (StringUtils.isEmpty(req.getActId())) {
            throw new ServiceException(MyErrorCode.ERROR_INPUT);
        }
        return whiskeyGiftsService.queryAwardRecord(req.getActId());
    }

    /**
     * 威士忌之星活动首页
     */
    @PostMapping("/home-Page")
    @ApiOperation(value = "威士忌之星活动首页查询", tags = {"威士忌之星活动首页"}, notes = "威士忌之星活动首页查询")
    public ResponseEntity homePage() {
        log.info("威士忌之星活动首页查询");
        return whiskeyGiftsService.homePage();
    }
}
